CreateLeaveRequestCommandHandler   A
last analyzed

Complexity

Total Complexity 3

Size/Duplication

Total Lines 87
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 3
eloc 66
dl 0
loc 87
rs 10
c 0
b 0
f 0

1 Function

Rating   Name   Duplication   Size   Complexity  
B execute 0 71 3
1
import { Inject } from '@nestjs/common';
2
import { CommandHandler } from '@nestjs/cqrs';
3
import { CreateLeaveRequestCommand } from './CreateLeaveRequestCommand';
4
import { ILeaveRequestRepository } from 'src/Domain/HumanResource/Leave/Repository/ILeaveRequestRepository';
5
import { LeaveRequest } from 'src/Domain/HumanResource/Leave/LeaveRequest.entity';
6
import { DoesLeaveRequestExistForPeriod } from 'src/Domain/HumanResource/Leave/Specification/DoesLeaveRequestExistForPeriod';
7
import { LeaveRequestAlreadyExistForThisPeriodException } from 'src/Domain/HumanResource/Leave/Exception/LeaveRequestAlreadyExistForThisPeriodException';
8
import { DoesLeaveExistForPeriod } from 'src/Domain/FairCalendar/Specification/DoesLeaveExistForPeriod';
9
import { EventsOrLeavesAlreadyExistForThisPeriodException } from 'src/Domain/FairCalendar/Exception/EventsOrLeavesAlreadyExistForThisPeriodException';
10
import { ICommandBus } from 'src/Application/ICommandBus';
11
import { CreateNotificationCommand } from 'src/Application/Notification/Command/CreateNotificationCommand';
12
import { NotificationType } from 'src/Domain/Notification/Notification.entity';
13
import { ITranslator } from 'src/Infrastructure/Translations/ITranslator';
14
import { ConfigService } from '@nestjs/config';
15
import { IDateUtils } from 'src/Application/IDateUtils';
16
17
@CommandHandler(CreateLeaveRequestCommand)
18
export class CreateLeaveRequestCommandHandler {
19
  constructor(
20
    @Inject('ILeaveRequestRepository')
21
    private readonly leaveRequestRepository: ILeaveRequestRepository,
22
    @Inject('ICommandBus')
23
    private readonly commandBus: ICommandBus,
24
    private readonly doesLeaveRequestExistForPeriod: DoesLeaveRequestExistForPeriod,
25
    private readonly doesLeaveExistForPeriod: DoesLeaveExistForPeriod,
26
    @Inject('ITranslator')
27
    private readonly translator: ITranslator,
28
    private readonly configService: ConfigService,
29
    @Inject('IDateUtils')
30
    private readonly dateUtils: IDateUtils
31
  ) {}
32
33
  public async execute(command: CreateLeaveRequestCommand): Promise<string> {
34
    const {
35
      user,
36
      endDate,
37
      endsAllDay,
38
      type,
39
      startDate,
40
      startsAllDay,
41
      comment
42
    } = command;
43
44
    if (
45
      true ===
46
      (await this.doesLeaveRequestExistForPeriod.isSatisfiedBy(
47
        user,
48
        startDate,
49
        endDate
50
      ))
51
    ) {
52
      throw new LeaveRequestAlreadyExistForThisPeriodException();
53
    }
54
55
    if (
56
      true ===
57
      (await this.doesLeaveExistForPeriod.isSatisfiedBy(
58
        user,
59
        startDate,
60
        endDate
61
      ))
62
    ) {
63
      throw new EventsOrLeavesAlreadyExistForThisPeriodException();
64
    }
65
66
    const leaveRequest = await this.leaveRequestRepository.save(
67
      new LeaveRequest(
68
        user,
69
        type,
70
        startDate,
71
        startsAllDay,
72
        endDate,
73
        endsAllDay,
74
        comment
75
      )
76
    );
77
78
    this.commandBus.execute(
79
      new CreateNotificationCommand(
80
        NotificationType.POST,
81
        this.translator.translate(
82
          'leave-requests-create-notification-message',
83
          {
84
            userFirstName: leaveRequest.getUser().getFirstName(),
85
            startDate: leaveRequest.getStartDate(),
86
            endDate: leaveRequest.getEndDate(),
87
            duration: this.dateUtils.getLeaveDuration(
88
              leaveRequest.getStartDate(),
89
              leaveRequest.isStartsAllDay(),
90
              leaveRequest.getEndDate(),
91
              leaveRequest.isEndsAllDay()
92
            ),
93
            link: `${this.configService.get<string>(
94
              'PERMACOOP_BASE_URL'
95
            )}/app/people/leaves/${leaveRequest.getId()}`
96
          }
97
        ),
98
        leaveRequest
99
      )
100
    );
101
102
    return leaveRequest.getId();
103
  }
104
}
105